package com.pctx.registerlogin.service.impl;

import com.pctx.common.result.Result;
import com.pctx.common.util.MD5Util;
import com.pctx.registerlogin.dao.CompanyMapper;
import com.pctx.registerlogin.dao.DepartmentMapper;
import com.pctx.registerlogin.dao.PositionMapper;
import com.pctx.registerlogin.dao.UserMapper;
import com.pctx.registerlogin.pojo.Company;
import com.pctx.registerlogin.pojo.Department;
import com.pctx.registerlogin.pojo.Position;
import com.pctx.registerlogin.pojo.User;
import com.pctx.registerlogin.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;
@Service("userService")
public class UserServiceImpl implements UserService{

    @Autowired
    private DepartmentMapper departmentMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private PositionMapper positionMapper;
    @Autowired
    private CompanyMapper companyMapper;
    @Override
    public Result addDepartment(String departmentname, String username) {
        User user = userMapper.selectByUsername(username);
        Department department = new Department();
        UUID uuid = UUID.randomUUID();
        department.setUuid(uuid.toString());
        department.setDepartmentName(departmentname);
        department.setCompanyUuid(user.getCompanyUuid());
        Date date = new Date();
        //Timestamp nowdate = new Timestamp(date.getTime());
        department.setCreateTime(date);
        department.setUpdateTime(date);
        Boolean flag = departmentMapper.insert(department) > 0;
        if (flag) {
            return Result.success(department, "部门建立成功");
        } else {
            return Result.error("部门建立失败");
        }

    }

   /* @Override
    public Result addPosition(String positionname, String departmentname, String username) {
        User user = userMapper.selectByUsername(username);
        Department department = departmentMapper.selectByname(departmentname);
        Position position = new Position();
        position.setCompanyUuid(user.getCompanyUuid());
        UUID uuid = UUID.randomUUID();
        position.setUuid(uuid.toString());
        position.setPositionName(positionname);
        position.setDepartmentUuid(department.getUuid());
        Date date = new Date();
        position.setCreateTime(date);
        position.setUpdateTime(date);
        Boolean flag = positionMapper.insert(position) > 0;
        if (flag) {
            return Result.success(null, "职位建立成功");
        } else {
            return Result.error("职位建立失败");
        }
    }*/

    @Override
    public Result addPositionone(String positionname, String departmentuuid, String username) {
        //User user = userMapper.selectByUsername(username);
        Department department=departmentMapper.selectByUuid(departmentuuid);
        Position position = new Position();
        position.setCompanyUuid(department.getCompanyUuid());
        UUID uuid = UUID.randomUUID();
        position.setUuid(uuid.toString());
        position.setPositionName(positionname);
        position.setDepartmentUuid(departmentuuid);
        Date date = new Date();
        position.setCreateTime(date);
        position.setUpdateTime(date);
        Boolean flag = positionMapper.insert(position) > 0;
        if (flag) {
            return Result.success(position, "职位建立成功");
        } else {
            return Result.error("职位建立失败");
        }
    }

    @Override
    public Result selectDepartment(String username) {
        User user = userMapper.selectByUsername(username);
        List<Department> list = departmentMapper.selectByCompanyuuid(user.getCompanyUuid());
        ArrayList listone = new ArrayList();
        for (int i = 0; i < list.size(); i++) {
            listone.add(list.get(i).getDepartmentName());
        }
        return Result.success(listone);
    }


    @Override
    public Result selectPosition(String username, String departmentuuid) {
        User user = userMapper.selectByUsername(username);
        List<Position> list = positionMapper.selectByDepartmentuuid(departmentuuid);
        ArrayList listtwo = new ArrayList();
        for (int i = 0; i < list.size(); i++) {
            listtwo.add(list.get(i).getPositionName());
        }
        return Result.success(listtwo);
    }

   /* @Override
    public Result addUser(String username, String name, String mobile, String mail, String sex, String departmentname, String positionname, String upperleader, String subordinate, String loginuser) {
        User userone = userMapper.selectByUsername(loginuser);
        UUID uuid = UUID.randomUUID();
        User user  = new User();
        user.setUuid(uuid.toString());
        user.setUsername(username);
        user.setName(name);
        user.setPassword(MD5Util.getMD5("666666"));
        user.setRealpassword("666666");
        user.setMail(mail);
        user.setMobile(mobile);
        if(sex.equals("男"))
            user.setSex(1);
        else
            user.setSex(2);
        Department department = departmentMapper.selectByname(departmentname);
        user.setDepartmentUuid(department.getUuid());
        Position position=positionMapper.selectByname(positionname);
        user.setPositionUuid(position.getUuid());
        if(subordinate.equals("无"))
            user.setSubordinate(0);
        else
            user.setSubordinate(1);
         user.setCompanyUuid(userone.getCompanyUuid());
         User usertwo=userMapper.selectByUsername(upperleader);
         if(usertwo.getDepartmentUuid().equals(department.getUuid()))
         user.setUpperUuid(usertwo.getUuid());
        Date date = new Date();
        user.setCreateTime(date);
        user.setUpdateTime(date);
        return Result.success(null, "用户创建成功");
    }*/

    @Override
    public Result addUser(String username, String name, String mobile, String mail, String sex, String departmentuuid, String positionuuid, String upperleader, String subordinate) {
       // User userone = userMapper.selectByUsername(loginuser);
        UUID uuid = UUID.randomUUID();
        User user  = new User();
        user.setUuid(uuid.toString());
        user.setUsername(username);
        user.setName(name);
        user.setPassword(MD5Util.getMD5("666666"));
        user.setRealpassword("666666");
        user.setMail(mail);
        user.setMobile(mobile);
        if(sex.equals("男"))
            user.setSex(1);
        else
            user.setSex(2);
        Department department = departmentMapper.selectByUuid(departmentuuid);
        user.setDepartmentUuid(departmentuuid);
       // Position position=positionMapper.selectByname(positionname);
        user.setPositionUuid(positionuuid);
        if(subordinate.equals("无"))
            user.setSubordinate(0);
        else
            user.setSubordinate(1);
        user.setCompanyUuid(department.getCompanyUuid());
        User usertwo=userMapper.selectByUsername(upperleader);
        if(usertwo.getDepartmentUuid().equals(departmentuuid))
            user.setUpperUuid(usertwo.getUuid());
        Date date = new Date();
        user.setCreateTime(date);
        user.setUpdateTime(date);
        Boolean flag=userMapper.insert(user)>0;
        if(flag)
        return Result.success(user, "用户创建成功");
        else
            return Result.error("用户创建失败");
    }


    @Override
    public Result deleteUser(String username) {
        Boolean flag=userMapper.deleteByname(username)>0;
        if(flag)
            return Result.success(null,"成功删除用户");
        else
            return Result.error("删除失败");

    }

    @Override
    public Result updateUser(String username, String name, String mobile, String mail, String sex, String departmentuuid, String positionuuid, String upperleaderuuid, String subordinate) {
        User user = userMapper.selectByUsername(username);
       // user.setUsername(username);
        user.setName(name);
        user.setMobile(mobile);
        user.setMail(mail);
        if(sex.equals("男"))
            user.setSex(1);
        else
            user.setSex(2);
        Department department = departmentMapper.selectByUuid(departmentuuid);
        user.setDepartmentUuid(department.getUuid());
        Position position=positionMapper.selectByUuid(positionuuid);
        user.setPositionUuid(position.getUuid());
        //User usertwo=userMapper.selectByUpperleaderuuid(upperleaderuuid);
       user.setUpperUuid(upperleaderuuid);
        if(subordinate.equals("无"))
            user.setSubordinate(0);
        else
            user.setSubordinate(1);
        Date date = new Date();
        user.setUpdateTime(date);
        Boolean flag=userMapper.update(user)>0;
        if(flag)
            return Result.success(user,"修改成功");
        else
            return Result.error("修改失败");

    }

    @Override
    public Result resetPassword(String username) {
        User user = userMapper.selectByUsername(username);
        user.setRealpassword("666666");
        user.setPassword(MD5Util.getMD5("666666"));
        Date date = new Date();
        user.setUpdateTime(date);
        Boolean flag=userMapper.updateByusername(user)>0;
        if(flag)
            return Result.success(user,"密码重置成功");
        else
            return Result.error("密码重置失败");
    }

    @Override
    public Result hassubordinate(String loginuser) {
        User user = userMapper.selectByUsername(loginuser);
        List<User> list=userMapper.selectHasSubordinate(user.getCompanyUuid());
        ArrayList listone=new ArrayList();
        for(int i=0;i<list.size();i++)
        {
            if(list.get(i).getSubordinate()==1)
                listone.add(list.get(i).getName());
        }
        return Result.success(listone);
    }

    @Override
    public Result personalMessage(String username) throws Exception {

            User user=userMapper.selectByUsername(username);
          Company company=companyMapper.selectByUuid(user.getCompanyUuid());
            Department department=departmentMapper.selectByUuid(user.getDepartmentUuid());
            Position position=positionMapper.selectByUuid(user.getPositionUuid());
            user.setDepartment(department);
        user.setCompany(company);
            user.setPosition(position);
            return Result.success(user,"成功");
    }


}